package 字符串的相关api;

import java.util.HashMap;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/1/8 13:35
 */
public class leetcode6284 {

    public static void main(String[] args) {
        String s1 = "abcc";
        String s2 = "aab";
        System.out.println(isItPossible1(s1,s2));
    }



/*
 这种题 真的服气
 恰好交换一次的意思就是，
 必须有 一个次， 而且只能 有有一次！
 */

    public static boolean isItPossible2(String word1, String word2) {
        int [][] mem = new int [26][2];
        int count1 = 0,count2 = 0;
        for(int i = 0;i < word1.length();i++){
            if(mem[word1.charAt(i) - 'a'][0] == 0){
                count1++;
            }
            mem[word1.charAt(i) - 'a'][0]++;
        }
        for(int i = 0;i < word2.length();i++){
            if(mem[word2.charAt(i) - 'a'][1] == 0){
                count2++;
            }
            mem[word2.charAt(i) - 'a'][1]++;
        }
        if(count1 == count2){
            return true;
        }
        if(Math.abs(count1 - count2) > 2){
            return false;
        }
        for(int i = 0;i < 26;i++){
            for(int j = 0;j < 26;j++){
                if(mem[j][1] > 1 && (count1 + (mem[j][0] == 0 ? 1 :0) == count2 + (mem[i][1] == 0 ? 1 : 0))){
                    return true;
                }
            }
        }

        return false;
    }

    public static boolean isItPossible1(String word1, String word2) {
        int [][] mem = new int [26][2];
        int count1 = 0,count2 = 0;
        for(int i = 0;i < word1.length();i++){
            if(mem[word1.charAt(i) - 'a'][0] == 0){
                count1++;
            }
            mem[word1.charAt(i) - 'a'][0]++;
        }
        for(int i = 0;i < word2.length();i++){
            if(mem[word2.charAt(i) - 'a'][1] == 0){
                count2++;
            }
            mem[word2.charAt(i) - 'a'][1]++;
        }
        if(count1 == count2){
            return true;
        }
        if(Math.abs(count1 - count2) > 2){
            return false;
        }
        for(int i = 0;i < 26;i++){
            int temp1 = count1,temp2 = count2;
            if(mem[i][0] == 0 && mem[i][1] >= 2){
                temp1++;
            }
            else if(mem[i][0] >= 2 && mem[i][1] == 0){
                temp2++;
            }
            if(temp1 == temp2){
                return true;
            }
        }
        return false;
    }




    /*
     还是进行枚举
     */
    public boolean isItPossible(String word1, String word2) {
        HashMap<Character, Integer> map1 = new HashMap<>(), map2 = new HashMap<>();
        for (char c : word1.toCharArray()) {
            map1.put(c, map1.getOrDefault(c, 0) + 1);
        }
        for (char c : word2.toCharArray()) {
            map2.put(c, map2.getOrDefault(c, 0) + 1);
        }
        for (char c1 : map1.keySet()) {
            for (char c2 : map2.keySet()) {
                if (c1 == c2 ? map1.size() == map2.size()
                        : map1.size() - (map1.get(c1) > 1 ? 0 : 1) + (map1.containsKey(c2) ? 0 : 1) == map2.size()
                        - (map2.get(c2) > 1 ? 0 : 1) + (map2.containsKey(c1) ? 0 : 1)) {
                    return true;
                }
            }
        }
        return false;
    }
}
